package avicit.bdp.dgs.mdm.rest;

import avicit.bdp.dgs.mdm.dto.ColumnDTO;
import avicit.bdp.dgs.mdm.dto.DataDTO;
import avicit.bdp.dgs.mdm.service.DataManageService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-12-11
 * @类说明：DataManageRest
 * @修改记录：
 * @注意事项：
 * @主要功能：数据维护Rest接口
 */
@RestController
@Api(tags = "mdm", description = "数据维护Rest接口")
@RequestMapping("/api/bdp/dgs/mdm/data")
public class DataManageRest {
    @Autowired
    private DataManageService dataManageService;

    /**
     * 新增数据
     *
     * @param dataDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增数据")
    public ResponseMsg<String> addData(@RequestBody DataDTO dataDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.addData(dataDTO));

        return responseMsg;
    }

    /**
     * 删除/批量删除数据，多个id使用英文逗号隔开
     *
     * @param topicId 主题ID
     * @param id  数据ID
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除数据，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteData(@ApiParam(value = "tableId", name = "topicId") @RequestParam String topicId,
                                           @ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.deleteData(topicId, id));

        return responseMsg;
    }

    /**
     * 修改数据
     *
     * @param dataDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改数据")
    public ResponseMsg<Boolean> updateData(@RequestBody DataDTO dataDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.updateData(dataDTO));

        return responseMsg;
    }

    /**
     * 查询数据详情
     *
     * @param tableId 模型ID
     * @param dataId  数据ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询数据详情")
    public ResponseMsg<Map<String, Object>> getDataDetail(@ApiParam(value = "tableId", name = "tableId") @RequestParam String tableId,
                                              @ApiParam(value = "dataId", name = "dataId") @RequestParam String dataId) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.getDataDetail(tableId, dataId));

        return responseMsg;
    }

    /**
     * 查询数据列表
     *
     * @param topicId  主题ID
     * @param pageNo   页码
     * @param pageSize 分页大小
     * @return
     */
    @GetMapping(value = "/getDataList")
    public ResponseMsg<Map<String, Object>> getDataList(
            @ApiParam(value = "主题ID", name = "topicId")@RequestParam String topicId,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.getDataList(topicId, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 查询字段列表（新增数据时使用）
     *
     * @param topicId  主题ID
     * @return
     */
    @GetMapping(value = "/getColumnList")
    public ResponseMsg<List<ColumnDTO>> getColumnList(@ApiParam(value = "主题ID", name = "topicId")@RequestParam String topicId) {
        ResponseMsg<List<ColumnDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.getColumnList(topicId));

        return responseMsg;
    }

    /**
     * 修订/生效/失效
     *
     * @param dataDTO
     * @return
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "修订数据")
    public ResponseMsg<Boolean> updateStatus(@RequestBody DataDTO dataDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dataManageService.updateStatus(dataDTO));

        return responseMsg;
    }
}
